﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports Global_Visas_Business_Layer
Imports System.ComponentModel
Imports System.Data
Imports System.Text
Partial Public Class frmAdminAssessingAuthorities
    Dim slsList As DataTable
    Private Sub frmAdminAssessingAuthorities_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Dim StrSQL As String = String.Empty
        Call FillDestinations()
        Call SQL_Auths()


        Me.Txt_Shortname_Old.Visibility = Windows.Visibility.Hidden
        Me.Txt_FullName_Old.Visibility = Windows.Visibility.Hidden
        Me.Combo_SLS_Old.Visibility = Windows.Visibility.Hidden
        Me.txtShortName.IsEnabled = False
        Me.txtFullName.IsEnabled = False
        Me.drpSLS.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        FillDrpSLS()

        'StrSQL = "Select SLS_ID, SLS_Name from I_SL_StockLetterSets where SLS_TypeID = 4"
        'Call GblFunc_Populate_List(StrSQL, "AssessingAuth_SLSList", Form_FMA_AssessingAuths.Combo_SLS)
        'Call GblFunc_Populate_List(StrSQL, "AssessingAuth_SLSListOld", Form_FMA_AssessingAuths.Combo_SLS_Old)

        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub
    End Sub

    Private Sub FillDestinations()
        Dim SQL As New StringBuilder
        With SQL
            .Append("select Country_ID,Country_Name from L_country  ")
            .Append(" where Country_Name like 'australia%' or ")
            .Append(" Country_Name like 'south af%' or ")
            .Append("    Country_Name like 'canada%' or ")
            .Append(" Country_Name like 'india' or ")
            .Append(" Country_Name like 'new zealand%' or ")
            .Append(" Country_Name like 'USA%' or ")
            .Append(" Country_Name like 'United Kingdom'")
        End With
        Try
            Dim destinationList As DataTable = CommonDataFunctions.GetSQLDatatable(SQL.ToString())
            With Me.drpDestination
                .SelectedValuePath = "Country_ID"
                .DisplayMemberPath = "Country_Name"
                .ItemsSource = CType(destinationList, IListSource).GetList
            End With
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAssessingAuthorities-FillDestinations")
            Exit Sub
        End Try

    End Sub

    Private Sub FillDrpSLS()
        Try
            Dim StrSQL As String = "Select SLS_ID, SLS_Name from I_SL_StockLetterSets where SLS_TypeID = 4"
            slsList = CommonDataFunctions.GetSQLDatatable(StrSQL.ToString())
            Dim slsList2 As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL.ToString())
            With Me.drpSLS
                .SelectedValuePath = "SLS_ID"
                .DisplayMemberPath = "SLS_Name"
                .ItemsSource = CType(slsList, IListSource).GetList
            End With
            With Me.Combo_SLS_Old
                .SelectedValuePath = "SLS_ID"
                .DisplayMemberPath = "SLS_Name"
                .ItemsSource = CType(slsList2, IListSource).GetList
            End With
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAssessingAuthorities-FillDrpSLS")
            Exit Sub
        End Try

    End Sub

    Public Sub SQL_Auths()
        Dim StrSQL As String

        StrSQL = "SELECT AssessingAuth_ID, AssessingAuth_ShortName as Authority "
        StrSQL = StrSQL & " FROM L_AssessingAuths"
        If drpDestination.SelectedIndex <> -1 Then
            StrSQL = StrSQL & " WHERE AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue
        Else : Exit Sub
        End If
        StrSQL = StrSQL & " ORDER BY AssessingAuth_ShortName"
        Dim authorityList As DataTable = Nothing
        Try
            authorityList = CommonDataFunctions.GetSQLDatatable(StrSQL.ToString())
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAssessingAuthorities-SQL_Auths")
        End Try
        Me.grdAuthority.ItemsSource = authorityList
        grdAuthority.Columns(0).IsVisible = False
        grdAuthority.Columns(1).Width = New GridLength(150)
        'Me.List_Auths = Null
        Me.txtFullName.Text = String.Empty
        Me.txtFullName.IsEnabled = False
        Me.txtShortName.Text = String.Empty
        Me.txtShortName.IsEnabled = False

        'Me.drpSLS.ItemsSource = Nothing
        Me.drpSLS.IsEnabled = False
        Me.grdOccupations.ItemsSource = Nothing

        Me.grdOccupations.ItemsSource = Nothing
        Me.grdOccupations.IsEnabled = False



        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False

       


    End Sub

    Private Sub drpDestination_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpDestination.SelectionChanged
        Call SQL_Auths()
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub grdAuthority_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdAuthority.SelectionChanged
        Dim StrSQL As String, Result As DataTable = Nothing
        Dim selectedAuthority As DataRow = grdAuthority.SelectedItem
        StrSQL = "SELECT AssessingAuth_ID, AssessingAuth_ShortName, AssessingAuth_Name, AssessingAuth_SLSID"
        StrSQL = StrSQL & " FROM L_AssessingAuths"
        StrSQL = StrSQL & " WHERE (AssessingAuth_ID = " & selectedAuthority(0) & ")"
        Try
            Result = CommonDataFunctions.GetSQLDatatable(StrSQL)
            If Result.Rows(0)("AssessingAuth_SLSID") = 0 Then
                'Me.Frm_SLS = 1
                Me.drpSLS.IsEnabled = False
                Me.drpSLS.SelectedValue = 0
                Me.radNoLetters.IsChecked = True
            Else
                'Me.Frm_SLS = 2
                Me.drpSLS.IsEnabled = True
                'Me.Combo_SLS = Rst.Fields("AssessingAuth_SLSID")
                Me.radStockLetters.IsChecked = True
                Me.drpSLS.SelectedValue = Result.Rows(0)("AssessingAuth_SLSID")
            End If

            Me.Txt_Shortname_Old.Text = Result.Rows(0)("AssessingAuth_Shortname")
            Me.Txt_FullName_Old.Text = Result.Rows(0)("AssessingAuth_Name")
            Me.Combo_SLS_Old.SelectedValue = Result.Rows(0)("AssessingAuth_SLSID")
            Me.txtShortName.Text = Result.Rows(0)("AssessingAuth_Shortname")
            Me.txtFullName.Text = Result.Rows(0)("AssessingAuth_Name")
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAssessingAuthorities-SQL_Auths")
            Exit Sub
        End Try

        Result.Dispose()

        Me.txtFullName.IsEnabled = True
        Me.txtShortName.IsEnabled = True
        Me.drpSLS.IsEnabled = True


        StrSQL = "SELECT L_SkilledOccAssAuth.SkilledOccAssAuth_ID, L_SkilledOcc.SkilledOcc_Title as 'Skill Title', L_SkilledCategories.SkilledCategory_Name as 'Skill Name'"
        StrSQL = StrSQL & " FROM L_SkilledOccAssAuth INNER JOIN"
        StrSQL = StrSQL & " L_SkilledOcc ON L_SkilledOccAssAuth.SkilledOccAssAuth_SkilledOccID = L_SkilledOcc.SkilledOcc_ID INNER JOIN"
        StrSQL = StrSQL & " L_SkilledCategories ON L_SkilledOcc.SkilledOcc_CategoryID = L_SkilledCategories.SkilledCategory_ID"
        StrSQL = StrSQL & " WHERE L_SkilledOccAssAuth.SkilledOccAssAuth_AssAuthID = " & selectedAuthority(0)

        ' Call GPL(StrSQL, "AssAuth_SkilledOCCList", Form_FMA_AssessingAuths.List_Occupations)
        Dim occupationList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        Me.grdOccupations.ItemsSource = occupationList
        Me.grdOccupations.Columns(0).IsVisible = False
        Me.grdOccupations.Columns(1).Width = New GridLength(150)
        Me.grdOccupations.Columns(2).Width = New GridLength(150)
        Me.grdOccupations.IsEnabled = True
        Me.btnEdit.IsEnabled = True
        Me.btnDelete.IsEnabled = True
        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub
    End Sub

    Private Sub radNoLetters_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radNoLetters.Checked
        Me.drpSLS.IsEnabled = False
    End Sub

    Private Sub radStockLetters_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radStockLetters.Checked
        Me.drpSLS.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        Dim StrSQL As String, StrMess As String, IntDirty As Integer, Rst As DataTable, StrOld As String

        If Me.btnEdit.Content = "Edit" Then
            'Enter Edit mode
            Me.grdAuthority.IsEnabled = True
            Me.drpDestination.IsEnabled = True
            Me.grdOccupations.IsEnabled = False
            Me.btnAddNew.IsEnabled = False
            Me.btnCancel.IsEnabled = True
            Me.btnEdit.Content = "Save"
            Me.btnDelete.IsEnabled = False

            Me.txtFullName.IsEnabled = True
            Me.txtShortName.IsEnabled = True
            Me.drpSLS.IsEnabled = True
            'Me.Frm_SLS.Locked = False
        Else
            'Validate

            If Me.txtFullName.Text = String.Empty Then
                Call CommonFuncs.DisplayMessage("Full name cannot be blank", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtFullName.Focus()
                Exit Sub
            End If

            If Me.txtShortName.Text = String.Empty Then
                Call CommonFuncs.DisplayMessage("Short name cannot be blank", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtShortName.Focus()
                Exit Sub
            End If

            If Me.drpSLS.SelectedIndex = -1 Or Me.drpSLS.SelectedValue = 0 Then
                Call CommonFuncs.DisplayMessage("Please select a Stock Letter Set, or set to 'No Stock Letters'", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpSLS.Focus()
                Exit Sub
            End If
            If drpDestination.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a destination.", CommonDialog.CustomDialogIcons.Information, "Select")
                Exit Sub
            End If
            If radStockLetters.IsChecked = True And drpSLS.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a Stock Letter.", CommonDialog.CustomDialogIcons.Information, "Select")
                Exit Sub
            End If

            Dim selectedRow As DataRow = Me.grdAuthority.SelectedItem
            StrSQL = "SELECT AssessingAuth_ID"
            StrSQL = StrSQL & " FROM L_AssessingAuths"
            StrSQL = StrSQL & " WHERE (AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue & ")"
            StrSQL = StrSQL & " AND (AssessingAuth_ShortName = '" & Me.txtShortName.Text.Trim & "')"
            StrSQL = StrSQL & " AND (AssessingAuth_ID <> " & selectedRow(0) & ")"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                Rst.Dispose()
                Call CommonFuncs.DisplayMessage("There is already a authority with that shortname.", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtShortName.Focus()
                Exit Sub
            End If

            StrSQL = "SELECT AssessingAuth_ID"
            StrSQL = StrSQL & " FROM L_AssessingAuths"
            StrSQL = StrSQL & " WHERE (AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue & ")"
            StrSQL = StrSQL & " AND (AssessingAuth_Name = '" & Me.txtFullName.Text & "')"
            StrSQL = StrSQL & " AND (AssessingAuth_ID <> " & selectedRow(0) & ")"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                Rst.Dispose()
                Call CommonFuncs.DisplayMessage("There is already a authority with that full name.", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtFullName.Focus()
                Exit Sub
            End If

            'Save changes
            IntDirty = 0
            StrSQL = "Update L_AssessingAuths Set "
            'StrMess = "Assessing Auth no. " & Me.List_Auths & " (" & Me.List_Auths.Column(1) & ") for destination " & Me.Combo_Destination.Column(1) & " edited as follows." & vbCrLf & vbCrLf

            If Me.txtFullName.Text.Trim <> Me.Txt_FullName_Old.Text.Trim Then
                IntDirty = 1

                StrSQL = StrSQL & " AssessingAuth_Name = '" & Me.txtFullName.Text & "'"
                StrMess = "Fullname changed. " & vbCrLf

            End If

            If Me.txtShortName.Text.Trim <> Me.txtShortName.Text.Trim Then

                If IntDirty = 1 Then StrSQL = StrSQL & ", "

                IntDirty = 1

                StrSQL = StrSQL & " AssessingAuth_ShortName = '" & Me.txtShortName.Text & "'"
                StrMess = "Short name changed " & vbCrLf

            End If

            If (Me.Combo_SLS_Old.SelectedValue <> 0) Or (Me.drpSLS.SelectedValue <> Me.Combo_SLS_Old.SelectedValue) Then

                If Me.Combo_SLS_Old.SelectedValue = 0 Then
                    StrOld = "'No Stock Letters'"
                Else
                    'Dim item As ComboBoxItem = Me.Combo_SLS_Old.SelectedItem
                    StrOld = Me.Combo_SLS_Old.Text

                End If

                If IntDirty = 1 Then StrSQL = StrSQL & ", "

                IntDirty = 1

                If Me.radNoLetters.IsChecked = True Then
                    StrSQL = StrSQL & " AssessingAuth_SLSID = 0"
                    StrMess = StrMess & "SLS changed from " & StrOld & " to 'No Stock Letters'"
                Else
                    StrSQL = StrSQL & " AssessingAuth_SLSID = " & Me.drpSLS.SelectedValue
                    StrMess = StrMess & "SLS changed from " & StrOld & " to " & Me.drpSLS.Text
                End If
            End If

                If IntDirty = 0 Then
                    Call MsgBox("Nothing changed!", , "Error")
                    Exit Sub
                End If

                StrSQL = StrSQL & " Where AssessingAuth_ID = " & selectedRow(0)
            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then
                CommonFuncs.DisplayMessage("Data Canot be updated. Please contact IT or try again.", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            Else
                CommonFuncs.DisplayMessage(StrMess.ToString(), CommonDialog.CustomDialogIcons.Information, "Message")
            End If



                Call SQL_Auths()

                'Reset form
            Me.grdAuthority.IsEnabled = True
            Me.drpDestination.IsEnabled = True
                Me.grdOccupations.IsEnabled = True
                Me.btnAddNew.IsEnabled = True
                Me.btnCancel.IsEnabled = False
                Me.btnEdit.Content = "Edit"
                Me.btnDelete.IsEnabled = True

                Me.txtFullName.IsEnabled = True
                Me.txtShortName.IsEnabled = True
            Me.drpSLS.IsEnabled = True
            Me.drpSLS.SelectedIndex = -1
                'Me.Frm_SLS.Locked = True
            End If

        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click

        Call SQL_Auths()

        Me.grdAuthority.IsEnabled = False
        Me.grdAuthority.Focus()
        Me.drpDestination.IsEnabled = False

        Me.btnAddNew.IsEnabled = True
        Me.btnAddNew.Content = "Add New"
        Me.btnCancel.IsEnabled = False
        Me.btnEdit.Content = "Edit"
        Me.drpSLS.SelectedIndex = -1
        If Me.grdAuthority.ItemsSource Is Nothing Then
            Me.btnEdit.IsEnabled = False
            Me.btnDelete.IsEnabled = False
        Else
            Me.btnEdit.IsEnabled = True
            Me.btnDelete.IsEnabled = True
        End If

        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub

    End Sub

    Private Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click

        Dim StrSQL As String, Rst As DataTable, StrMess As String, LngAuth As Long

        If Me.btnAddNew.Content = "Add New" Then

            'FillDestinations()

            'Enter Edit mode
            If drpDestination.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select the destination.", CommonDialog.CustomDialogIcons.Information, "Select")
                Exit Sub
            End If
            Me.grdAuthority.IsEnabled = True
            Me.drpDestination.IsEnabled = True
            Me.grdOccupations.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = False
            Me.btnAddNew.Content = "Save"

            Me.txtFullName.IsEnabled = True
            Me.txtShortName.IsEnabled = True
            Me.drpSLS.IsEnabled = False
            'Me.Frm_SLS.Enabled = True
            Me.radNoLetters.IsChecked = True
            Me.txtFullName.Text = String.Empty
            Me.txtShortName.Text = String.Empty
            'Me.Frm_SLS = 1
            Me.drpSLS.SelectedIndex = -1
            Me.txtFullName.Focus()
        Else
            'Validate

            If Me.txtFullName.Text = String.Empty Then
                Call CommonFuncs.DisplayMessage("Full name cannot be blank", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtFullName.Focus()
                Exit Sub
            End If

            If Me.txtShortName.Text = String.Empty Then
                Call CommonFuncs.DisplayMessage("Short name cannot be blank", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtShortName.Focus()
                Exit Sub
            End If

            If (drpSLS.SelectedIndex = -1 And Me.radStockLetters.IsChecked = True) Then
                Call CommonFuncs.DisplayMessage("Please select a Stock Letter Set, or set to 'No Stock Letters'", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpSLS.Focus()
                Exit Sub
            End If
            If drpDestination.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a destination.", CommonDialog.CustomDialogIcons.Information, "Select")
                Exit Sub
            End If
            If radStockLetters.IsChecked = True And drpSLS.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a Stock Letter.", CommonDialog.CustomDialogIcons.Information, "Select")
                Exit Sub
            End If

            StrSQL = "SELECT AssessingAuth_ID"
            StrSQL = StrSQL & " FROM L_AssessingAuths"
            StrSQL = StrSQL & " WHERE AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue
            StrSQL = StrSQL & " AND AssessingAuth_ShortName = '" & Me.txtShortName.Text.Trim & "'"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                Rst.Dispose()
                Call CommonFuncs.DisplayMessage("There is already a authority with that shortname.", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtShortName.Focus()
                Exit Sub
            End If

            StrSQL = "SELECT AssessingAuth_ID"
            StrSQL = StrSQL & " FROM L_AssessingAuths"
            StrSQL = StrSQL & " WHERE AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue
            StrSQL = StrSQL & " AND AssessingAuth_Name = '" & Me.txtFullName.Text & "'"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                Rst.Dispose()
                Call CommonFuncs.DisplayMessage("There is already a authority with that full name.", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtFullName.Focus()
                Exit Sub
            End If

            'Save changes
            StrSQL = "Insert Into L_AssessingAuths (AssessingAuth_ShortName, AssessingAuth_Name, AssessingAuth_CountryID, Assessingauth_SLSID) "
            StrSQL = StrSQL & " VALUES ("
            StrSQL = StrSQL & " '" & Me.txtShortName.Text & "', "
            StrSQL = StrSQL & " '" & Me.txtFullName.Text & "', "
            StrSQL = StrSQL & Me.drpDestination.SelectedValue & ", "
            If Me.radNoLetters.IsChecked = True Then
                StrSQL = StrSQL & " 0 "
            Else
                StrSQL = StrSQL & Me.drpSLS.SelectedValue
            End If
            StrSQL = StrSQL & ")"

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then
                CommonFuncs.DisplayMessage("Error occurred saving the data. Please try again or Contact IT.", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If


            StrSQL = "SELECT AssessingAuth_ID"
            StrSQL = StrSQL & " FROM L_AssessingAuths"
            StrSQL = StrSQL & " WHERE (AssessingAuth_CountryID = " & Me.drpDestination.SelectedValue & ")"
            StrSQL = StrSQL & " AND (AssessingAuth_Name = '" & Me.txtFullName.Text & "')"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Call CommonFuncs.DisplayMessage("New Auth not found!", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            LngAuth = Rst.Rows(0)("AssessingAuth_ID")
            Rst.Dispose()

            StrMess = "New Assessing Auth added, no. " & LngAuth & " (" & Me.txtFullName.Text & ") for destination " & Me.drpDestination.Text & vbCrLf & vbCrLf
            StrMess = StrMess & "Shortname : " & Me.txtShortName.Text & vbCrLf
            If Me.radStockLetters.IsChecked = 1 Then
                StrMess = StrMess & "No Stock Letters"
            Else
                StrMess = StrMess & "Stock Letter Set : " & Me.drpSLS.Text
            End If

            'Call GblFunc_Add_SysEvent("Admin (Assess Auth)", StrMess)

            Call Me.SQL_Auths()

            'Call List_Auths_AfterUpdate()

            'Reset form
            Me.grdAuthority.Focus()
            'Me.grdAuthority.ise = False
            Me.drpDestination.IsEnabled = False
            Me.grdOccupations.IsEnabled = True
            Me.btnAddNew.IsEnabled = True
            Me.btnAddNew.Content = "Add New"
            Me.btnCancel.IsEnabled = False
            Me.btnEdit.Content = "Edit"
            Me.btnDelete.IsEnabled = True
            Me.drpSLS.SelectedIndex = -1
            Me.txtFullName.IsEnabled = True
            Me.txtShortName.IsEnabled = True
            Me.drpSLS.IsEnabled = True
            ' Me.Frm_SLS.Locked = True
        End If

        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub


    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click

        Dim StrSQL As String, Rst As DataTable, StrMess As String
        Dim selectedSuthority As DataRow = Me.grdAuthority.SelectedItem
        If selectedSuthority Is Nothing Then
            CommonFuncs.DisplayMessage("Please select an authority to delete.", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
        StrSQL = "Select SkilledOcc_ID from L_SkilledOcc where SkilledOcc_AuthId = " & selectedSuthority(0)
        Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

        If Rst.Rows.Count > 0 Then

            'Rst.MoveLast()
            StrMess = Rst.Rows.Count & " Skilled Occupations have this as an assessing Auth." & vbCrLf & "They need to be set to something else before it can be deleted."
            Rst.Dispose()

            Call CommonFuncs.DisplayMessage(StrMess, CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
        Rst.Dispose()

        StrMess = "Assessing Auth no. " & selectedSuthority(0) & " (" & selectedSuthority(1) & ") deleted"
        StrSQL = "Delete from L_AssessingAuths where AssessingAuth_ID = " & selectedSuthority(0)
        If CommonDataFunctions.ExecuteSQL(StrSQL) = True Then
            CommonFuncs.DisplayMessage(StrMess, CommonDialog.CustomDialogIcons.Information, "Error")
        Else
            CommonFuncs.DisplayMessage("Unable to delete the authority details from database. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If

        Me.drpSLS.SelectedIndex = -1
        SQL_Auths()

        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        Exit Sub


    End Sub
End Class
